package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LevelPrivilegeController {
    @Autowired
    LevelPrivilegeService levelPrivilegeService;

//    分页查询:getLevelPrivilegeList
    @GetMapping("/admin/privilege/list")
    @SaAdminCheckPermission("admin:privilege:search")
    public Result<PageDTO<LevelPrivilegeDTO>> levelPrivilegeList(LevelPrivilegePageRequest pageRequest) {
        PageDTO<LevelPrivilegeDTO> levelPrivilegeList = levelPrivilegeService.getLevelPrivilegeList(pageRequest);
        return Result.ok(levelPrivilegeList);
    }

//    添加等级特权：addLevelPrivilege
    @PostMapping("/admin/privilege")
    @SaAdminCheckPermission("admin:privilege:add")
    public Result<String> addLevelPrivilegeInfo(@RequestBody LevelPrivilegeCommand command) {
        if (command != null) {
            levelPrivilegeService.addLevelPrivilege(command);
            return Result.ok("");
        }
        return Result.fail();
    }

    // 更新等级特权：updateLevelPrivilege
    @PutMapping("/admin/privilege")
    @SaAdminCheckPermission("admin:privilege:update")
    public Result<String> updateLevelPrivilege(@RequestBody LevelPrivilegeCommand command) {
        if (command != null) {
            levelPrivilegeService.updateLevelPrivilege(command);
            return Result.ok("");
        }
        return Result.fail();
    }

//    更新等级特权：deleteLevelPrivilege
    @DeleteMapping("/admin/privilege/{id}")
    @SaAdminCheckPermission("admin:privilege:delete")
    public Result<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilegeOne(@PathVariable("id") Long id) {
        DeleteLevelPrivilegeOperateDTO deleteLevelPrivilegeOperateDTO = levelPrivilegeService.deleteLevelPrivilege(id);
        return Result.ok(deleteLevelPrivilegeOperateDTO);
    }

//    批量删除更新等级特权：batchDeleteLevelPrivilege
    @DeleteMapping("/admin/privilege/batch")
    @SaAdminCheckPermission("admin:privilege:batchdelete")
    public Result<List<DeleteLevelPrivilegeOperateDTO>> batchDeleteLevelPrivilege(@RequestBody BatchDeleteCommand command){
        if (command!=null) {
            List<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilegeOperateDTOS = levelPrivilegeService.batchDeleteLevelPrivilege(command);
            return Result.ok(deleteLevelPrivilegeOperateDTOS);
        }
        return Result.fail();
    }
}
